//
//  PropagandaTextPopView.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/31.
//  Copyright © 2020 lt. All rights reserved.
//
//推广文案弹窗
import Foundation

import UIKit

class CopyTextPopView: UIView {
    var copyBtnTouch: (() -> Void)?
    var copyTextLabel:UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        sudokuConstraints()
    }
   
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //MARK: - 布局
      func sudokuConstraints(){
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: 0, width: (300/375)*screenWidth, height: (210/375)*screenWidth)
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        layerView.layer.cornerRadius = 10;
        layerView.layer.masksToBounds = true
        self.addSubview(layerView)
        addTitleLabel(layerView)
        addTextBgView(layerView)
        addPasterButton(layerView)
     }
    
    func addTitleLabel(_ view:UIView){

        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "推广文案")
        label.frame = CGRect(x: (view.size.width-(150/375)*screenWidth)/2, y: (13/375)*screenWidth, width: (150/375)*screenWidth, height: 20)
        label.numberOfLines = 0
        label.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Medium", size: 14)!,.foregroundColor: UIColor(red: 0, green: 0, blue: 0,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
    
    func addTextBgView(_ view:UIView){
        let layerView = UIView()
        layerView.frame = CGRect(x: (view.size.width-(270/375)*screenWidth)/2, y: (43/375)*screenWidth, width: (270/375)*screenWidth, height: (95/375)*screenWidth)
        // layerFillCode
        layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
        let layer = CALayer()
        layer.frame = layerView.bounds
        layer.backgroundColor = UIColor(red: 0.98, green: 0.98, blue: 0.98, alpha: 1).cgColor
        layerView.layer.addSublayer(layer)
        layerView.layer.cornerRadius = 10;
        view.addSubview(layerView)
        
        copyTextLabel = UILabel()
        let attrString = NSMutableAttributedString(string: "")
        copyTextLabel.frame = CGRect(x: (layerView.size.width-(237/375)*screenWidth)/2, y: (layerView.size.height-(95/375)*screenWidth)/2, width: (237/375)*screenWidth, height: (95/375)*screenWidth)
        copyTextLabel.numberOfLines = 0
        copyTextLabel.textAlignment = .center
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang-SC-Regular", size: 14)!,.foregroundColor: UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        layerView.addSubview(copyTextLabel)
        copyTextLabel.attributedText = attrString
    }
    
  
    
    func  addPasterButton(_ view:UIView){
         let layerView = UIButton()
        layerView.frame = CGRect(x:(view.size.width - (240/375)*screenWidth)/2, y: (154/375)*screenWidth, width: (240/375)*screenWidth, height: (40/375)*screenWidth)
         // layerFillCode
         layerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
         let layer = CALayer()
         layer.frame = layerView.bounds
         layer.backgroundColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1).cgColor
         layerView.layer.addSublayer(layer)
         // gradientCode
         let gradient1 = CAGradientLayer()
         gradient1.colors = [UIColor(red: 0.67, green: 0.44, blue: 1, alpha: 1).cgColor, UIColor(red: 0.44, green: 0.33, blue: 1, alpha: 1).cgColor]
         gradient1.locations = [0, 1]
         gradient1.startPoint = CGPoint(x: 0.0, y: 0.5)
         gradient1.endPoint = CGPoint(x: 1.0, y: 0.5)
         gradient1.frame = layerView.bounds
         layerView.layer.addSublayer(gradient1)
         layerView.layer.cornerRadius = 20;
         layerView.layer.masksToBounds = true
         layerView.setTitle("复制", for: .normal)
         layerView.setTitleColor(UIColor.white, for: .normal)
         layerView.titleLabel?.font = .systemFont(ofSize: 14)
         layerView.addTarget(self, action: #selector(click2), for: .touchUpInside)
         view.addSubview(layerView)
    }
    
    @objc func click2(){
           copyBtnTouch?()
        }
}
